package br.com.cfccultura.admin.control.actions.login;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Component;

import br.com.cfccultura.admin.control.actions.DefaultAction;
import br.com.cfccultura.admin.control.forms.login.LoginForm;
import br.com.cfccultura.admin.model.dao.exceptions.SenhaInvalidaException;
import br.com.cfccultura.admin.model.dao.exceptions.UsuarioInativoException;
import br.com.cfccultura.admin.model.dao.exceptions.UsuarioInvalidoException;
import br.com.cfccultura.admin.model.enums.TipoFuncionario;
import br.com.cfccultura.admin.model.vo.Funcionario;

@Component("/login")
public class LoginAction extends DefaultAction {
	
	private static final Logger logger = Logger.getLogger(LoginAction.class); 
	
	public ActionForward iniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		return mapping.findForward("login");
	}
	
	public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		LoginForm loginForm = (LoginForm) form;
		
		try {
			Funcionario funcionario = new Funcionario();
			funcionario.setLogin(loginForm.getUsuario());
			funcionario.setSenha(loginForm.getSenha());
			Funcionario vo = getFuncionarioDAO().login(funcionario);		
			setUsuario(request, vo);

			setScreenLimit(loginForm, request);
			
			logger.info("Sucessfully logon to: " + loginForm.getUsuario());
			return mapping.findForward("home");
		} catch(EmptyResultDataAccessException exc) {
			addMessage(request, "login.usuario.invalido");
			logger.info("Invalid User to: " + loginForm.getUsuario());
		} catch(UsuarioInvalidoException exc) {
			addMessage(request, "login.usuario.invalido");
			logger.info("Invalid User to: " + loginForm.getUsuario());
		} catch (SenhaInvalidaException exc) {
			addMessage(request, "login.senha.invalida");
			logger.info("Invalid Password to: " + loginForm.getUsuario());
		} catch(UsuarioInativoException exc) {
			addMessage(request, "login.usuario.inativo");
			logger.info("Disabled User to: " + loginForm.getUsuario());
		}
		
		return mapping.findForward("login");
	}
	
	private void setScreenLimit(LoginForm loginForm, HttpServletRequest request) {
		
		Integer width = new Integer(loginForm.getScreenWidth());
		Integer height = new Integer(loginForm.getScreenHeight());
		
		logger.debug("Resolution -> " + width + " x " + height);
		
		if(height == 900) {
			height -= 230;
		} else if(height == 768) {
			height -= 240;
		} else if(height == 1080) {
			height -= 300;
		}
		
		request.getSession().setAttribute("screenWidth", width);
		request.getSession().setAttribute("screenHeight", height);
	}
	
	public ActionForward home(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {			

		request.getSession().removeAttribute("carrinhoServicos");
		
		if(getUsuario(request).getTipo().equals(TipoFuncionario.ATENDIMENTO)) {
			
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MONTH, 1);
			if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
				c.add(Calendar.DAY_OF_MONTH, 2);
			} else if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				c.add(Calendar.DAY_OF_MONTH, 1);
			}
			
			request.setAttribute("data30Cheque", getDateFormatDojo().format(c.getTime()));
			
			c.setTime(new Date());
			c.add(Calendar.DAY_OF_MONTH, 15);
			if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
				c.add(Calendar.DAY_OF_MONTH, 2);
			} else if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				c.add(Calendar.DAY_OF_MONTH, 1);
			}		
			
			request.setAttribute("data15Cheque", getDateFormatDojo().format(c.getTime()));
			request.setAttribute("acrescimo15Dias", 5);
			
			return mapping.findForward("homeAtendimento");	
		} else if(getUsuario(request).getTipo().equals(TipoFuncionario.ADMINISTRATIVO)) {
			return mapping.findForward("homeAdministrativo");	
		}
		return mapping.findForward("home");
	}
	
	public ActionForward sair(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		logger.info("Logoff: " + getUsuario(request));
		request.getSession().invalidate();
		
		return mapping.findForward("login");
	}
}
